package com.example.administrator.myndk;

import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class DNPlayer {
    static {
        //加载native中的类。 加载之后此类才能用native方法调用到native层方法
        System.loadLibrary("native-lib");
    }

    private String dataSource;
    private SurfaceView surfaceView;
    private OnPrepareListener listener;
    SurfaceHolder holder;

    /**
     * 设置播放文件或直播地址（将播放地址传入）
     */
    public void setDataSource(String dataSource) {
        this.dataSource = dataSource;
    }

    /**
     *将surfaceView传入
     */
    public void setSurfaceView(SurfaceView surfaceView) {
        holder = surfaceView.getHolder();
        //监听画布什么时候创建好   画布是系统创建不知道什么时候准备好所以要监听
        holder.addCallback(callback);


    }


    private SurfaceHolder.Callback callback = new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {

        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            native_setSurface(holder.getSurface());
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {

        }
    };

    /**
     * 准备好播放的视频
     */
    public void prepare() {
        native_prepare(dataSource);
    }

    public void onError(int error) {

    }

    public void onPrepare() {
        if (null != listener) {
            listener.onPrepare();
        }
    }

    public void setOnPrepareListener(OnPrepareListener listener) {
        this.listener = listener;
    }

    public interface OnPrepareListener {
        void onPrepare();
    }

    /**
     * 开始播放
     */
    public void start() {
        native_start();
    }

    /**
     * 停止播放
     */
    public void stop() {

    }

    public void release() {
        holder.removeCallback(callback);
    }


    native void native_prepare(String dataSource);

    native void native_start();

    native void native_setSurface(Surface surface);
}
